﻿//http://www.codeproject.com/KB/cs/SandcastleComponents.aspx - SandcastleBuilder.Components.CodeBlockComponent

#if DEBUG //only compile sample in debug mode
using System;
using System.Web.UI;
using Birkelund.QueryStrings.Interfaces;
using Birkelund.QueryStrings.Tokens;
using Birkelund.QueryStrings.Enums;

#pragma warning disable 1591 //disable warning for xml documentation

namespace Birkelund.QueryStrings.Documents.SampleCode
{
    internal class Demonstration
    {
        //sample 1
        #region Read a simple QueryString token
        public sealed class QueryStrings1
        {
            //Recommended to always use type IQueryStringToken<T> instead of concrete class.
            public static readonly IQueryStringToken<int> DocumentId = new ValueTypeToken<int>("did");
        }

        public class WebSite1 : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                // read the value from the url. If the key does not exist or if any error occurs, the default value will be returned
                string message = string.Format("Key '{0}' has value '{1}'", QueryStrings1.DocumentId.Key, QueryStrings1.DocumentId.ReadValue());
                Response.Write(message);
            }
        }
        #endregion

        //sample 2
        #region Read QueryString token - extented
        //nullabletypes
        #endregion

        //sample 3
        #region Write a simple QueryString token
        public sealed class QueryStrings3
        {
            public static readonly IQueryStringToken<int> DocumentId = new ValueTypeToken<int>("did");
            public static readonly IQueryStringToken<bool> DoSort = new ValueTypeToken<bool>("sort");
        }

        public class WebSite3 : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                UriWriter writer = UriWriter.CreateEmptyFromCurrentUrl();
                writer
                    .SetToken(QueryStrings3.DoSort, true) //note strongly typed assignment. Values like 1 or "true" will fail compiletime
                    .SetToken(QueryStrings3.DocumentId, 3);

                string uri = writer.GenerateQueryAsString();
                Response.Write(uri);
            }
        }
        #endregion

        //sample 4
        #region Write QueryString token - extented
        //token encryption
        #endregion

        //sample 5
        #region Restrict allowed values with validation
        public sealed class QueryStrings5
        {
            public static readonly IQueryStringToken<int> DocumentId = new ValueTypeToken<int>("did")
            {
                Validator = (input) => input > 0 && input < 10 //validates that the read value is between 0 and 10. This is a just delegate so anything is possible in here :-)
            };
        }

        public class WebSite5 : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                // if the value does not meet the validation criteria, the default value will be returned
                string message = string.Format("Key '{0}' has value '{1}'", QueryStrings5.DocumentId.Key, QueryStrings5.DocumentId.ReadValue());
                Response.Write(message);
            }
        }
        #endregion

        //sample 6
        #region Encrypt a single token
        public sealed class QueryStrings6
        {
            public static readonly IQueryStringToken<int> DocumentId = new ValueTypeTokenEncrypted<int>("did");
            public static readonly IQueryStringToken<bool> DoSort = new ValueTypeToken<bool>("sort");
        }

        public class WebSite6 : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                UriWriter writer = UriWriter.CreateEmptyFromCurrentUrl();
                writer
                    .SetToken(QueryStrings6.DoSort, true)
                    .SetToken(QueryStrings6.DocumentId, 3);

                string uri = writer.GenerateQueryAsString();
                Response.Write(uri); //http://localhost/Default.aspx?did=RF3q3lM3fDQ%3d&sort=True
            }
        }
        #endregion

        //sample 7
        #region Encrypt the entire querystring
        public sealed class QueryStrings7
        {
            static QueryStrings7()
            {
                //Simply tell the framework that full (default) encryption should take place. An overload let you provide a custom encryption algorithm.
                QueryStringConfiguration.Instance.SetFullEncryption();

                //uncomment below to let the QueryString encryption be configurable
                //bool doEncryptEntireQueryString = true;

                //bool canParse = bool.TryParse(ConfigurationManager.AppSettings["DoEncryptEntireQueryString"], out doEncryptEntireQueryString);
                //if (canParse && doEncryptEntireQueryString)
                //{
                //    QueryStringConfiguration.Instance.SetFullEncryption();
                //}
            }

            public static readonly IQueryStringToken<int> DocumentId = new ValueTypeToken<int>("did");
            public static readonly IQueryStringToken<bool> DoSort = new ValueTypeToken<bool>("sort");
        }

        public class WebSite7 : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                UriWriter writer = UriWriter.CreateEmptyFromCurrentUrl();
                writer
                    .SetToken(QueryStrings3.DoSort, true)
                    .SetToken(QueryStrings3.DocumentId, 3);

                string uri = writer.GenerateQueryAsString();
                Response.Write(uri); //output will be similar to http://www.mysite.com/default.aspx?fe=Q1Fvcg5fTVZtqzzjQ2hT4g==
            }
        }
        #endregion

        //sample 8
        #region Manage exceptions (+ default values)
        public sealed class QueryStrings8
        {
            public static readonly IQueryStringToken<int> DocumentId = new ValueTypeToken<int>("did")
            {
                ExceptionNotificationBehaviour = ExceptionNotificationBehaviour.ThrowException
            };

            public static readonly IQueryStringToken<bool> DoSort = new ValueTypeToken<bool>("sort")
            {
                ExceptionNotificationBehaviour = ExceptionNotificationBehaviour.Silent,
                ReadFailureAction = RedirectToDefault
            };

            private static void RedirectToDefault(FailureArgs args)
            {
                //Log(args);
                //Redirect to default.aspx
            }
        }

        //GlobalAsax general catch of all token exceptions

        public class WebSite8 : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                // read the value from the url. If the key does not exist or if any error occurs, the default value will be returned
                string message = string.Format("Key '{0}' has value '{1}'", QueryStrings1.DocumentId.Key, QueryStrings1.DocumentId.ReadValue());
                Response.Write(message);
            }
        }
        #endregion

        //sample 9
        #region Using checksums
        public sealed class QueryStrings9
        {
            //Recommended to always use type IQueryStringToken<T> instead of concrete class.
            public static readonly IQueryStringToken<int> DocumentId = new ValueTypeToken<int>("did");
        }

        public class WebSite9 : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                UriWriter writer = UriWriter.CreateEmptyFromUrl("")
                    .SetToken(QueryStrings9.DocumentId, 99);

                writer.ApplyCheckSumControl = true;
                //writer.CalculateCheckSumAlgorithm = your own algorithm here

                Uri generated = writer.GenerateQueryAsUri();

                int did = QueryStrings9.DocumentId.ReadValue(generated, ReadValueMode.CheckSumControl); //this line will fail if the DocumentId is anything but 99. Usefull e.g. if personalized urls are provided in an email
            }
        }
        #endregion

        //sample 10
        #region Customizations (encryption, tokentypes)
        #endregion

        //sample 11
        #region Convert old style to new style

        #endregion
    }
}
#pragma warning restore 1591
#endif